//
// Copyright (c) 2001-2003 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees.  This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents.  Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and BroadVision One-To-One are registered
// trademarks of BroadVision, Inc., in the United States and the European
// Community, and are trademarks of BroadVision, Inc., in other
// countries.  The BroadVision logo, is a trademark of BroadVision, Inc.,
// in the United States and other countries. Additionally, IONA and Orbix
// are trademarks of IONA Technologies, Ltd.  RSA, MD5, and RC2 are
// trademarks of RSA Data Security, Inc.
//

function validateNumericField(isMoney, allowNegative, wholeNumber, formElement)
{
   var enterNumber = '\u6709\u52b9\u306a\u6570\u5b57\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   var enterPositiveNumber = '\u6b63\u6570\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   var enterWholeNumber = '\u6574\u6570\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   var enterPositiveWholeNumbe = '\u6b63\u306e\u6574\u6570\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   var msg;
   if (allowNegative) {
      if (wholeNumber)   msg = enterWholeNumber;
      else msg = enterNumber;
   } else {//only positive is acceptible
      if (wholeNumber) msg = enterPositiveWholeNumbe;
      else msg = enterPositiveNumber;
   }

   //var fieldName = formElement.friendlyName || "" ;

   val = formElement.value.stringTrim() ;
   if (val.length == 0) return true ;

   //  money validation was removed for internationalization
    
   var stringContainsNonNumbers = val.search(new RegExp("[^0-9.,-]", "g")) >= 0 ;
    
   val = parseFloat(val) ;
   if (isNaN(val) || stringContainsNonNumbers) {
      if (isMoney) {
         var tmpMsg = '\u6709\u52b9\u306a\u6570\u5b57\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
         tmpMsg += ' ' + '\u901a\u8ca8\u6587\u5b57\u3068\u304b\u5343\u30c7\u30ea\u30df\u30bf\u3092\u542b\u307e\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002';
         alert(tmpMsg) ;
      } else {
         alert('\u6709\u52b9\u306a\u6570\u5b57\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002') ;
      }      
      
      formElement.focus() ;
      return false ;
   }

   if ((!allowNegative && val < 0) || (wholeNumber && val != Math.round(val))) {
      alert(msg) ;
      formElement.focus() ;
      return false ;
   }

   return true ;
}



function validateNumericFields(isMoney, allowNegative, wholeNumber)
{
   var fieldName = null, len = null ;
   var enterNumber = '\u6709\u52b9\u306a\u6570\u5b57\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   var enterPositiveNumber = '\u6b63\u6570\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   var enterWholeNumber = '\u6574\u6570\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   var enterPositiveWholeNumbe = '\u6b63\u306e\u6574\u6570\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   var msg;
   if (allowNegative) {
      if (wholeNumber)   msg = enterWholeNumber;
      else msg = enterNumber;
   } else {//only positive is acceptible
      if (wholeNumber) msg = enterPositiveWholeNumbe;
      else msg = enterPositiveNumber;
   }

   for (var i = 3; i < arguments.length; i++) {
      formElement = arguments[i] ;
   
      len = formElement.length ;
      
      if (len == null) {
         if (!validateNumericField(isMoney, allowNegative, wholeNumber, formElement)) 
            return false ;
      } else {
         for (var y = 0; y < len; y++)
            if (!validateNumericField(isMoney, allowNegative, wholeNumber, formElement[y])) 
               return false ;
      }
   }

   return true ;
}



function validateMoneyField(formElement) 
{
   var val = null  ;
   decallowed = 2;  // how many decimals are allowed?

   val = formElement.value.stringTrim() ;
   var fieldName = formElement.friendlyName ;

   // remove "$" and ","
   val = val.replace(/^\$/g, "").replace(/,/g, "") ;
   
   var msg = '\u6709\u52b9\u306a\u6570\u5b57\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
   msg += ' ' + '\u901a\u8ca8\u6587\u5b57\u3068\u304b\u5343\u30c7\u30ea\u30df\u30bf\u3092\u542b\u307e\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002';
   if (isNaN(val) || val == "") {
      alert(msg);
      formElement.select();
      formElement.focus();

      return false ;
   } else {
      if (val.indexOf('.') == -1) val += ".";
      var  dectext = val.substring(val.indexOf('.')+1, val.length);

      if (dectext.length > decallowed) {
         var tmpMsg = '\u6570\u5b57\u3092\u5c0f\u6570\u70b9\u7b2c{0}\u4f4d\u3067\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002';
         tmpMsg = tmpMsg.replace(/\{0\}/g, decallowed);
         alert (tmpMsg);
         formElement.select();
         formElement.focus();

         return false ;
      }
   }

   return true ;
}


//  remove $ and , from a string
function filterNum(str) {
   re = new RegExp("^\$|,","g") ;

   // remove "$" and ","
   return str.replace(re, "");
}



function checkAllCheckboxes(checkboxField, img) {
   if (!checkboxField) return ;
   var value = img.src.indexOf("/selling/images/app/icon_check_all.gif") >= 0 ;
   
   img.src = (value?"/selling/images/app/icon_check_none.gif":
                     "/selling/images/app/icon_check_all.gif") ;
                     
   
   if (checkboxField.length == null) checkboxField.checked = value ;
   
   for (i = 0; i < checkboxField.length; i++)
      checkboxField[i].checked = value ;
}

function setFocus(theForm, field) 
{
   for (var i = 0; i < theForm.elements.length; i++)
   {
      if (theForm.elements[i].name == field) {
         theForm.elements[i].focus() ;
         return true ;
      }
   }

   return false ;
}

function clearForm(theForm) {
   if (confirm('\u3059\u3079\u3066\u306e\u30d5\u30a3\u30eb\u30c9\u3092\u30af\u30ea\u30a2\u3057\u307e\u3059\u304b\u3002')) {
      for (var i = 0; i < theForm.elements.length; i++ ) {

         if (theForm.elements[i].type != 'submit' &&
                  theForm.elements[i].type != 'button' &&
                  theForm.elements[i].type != 'hidden')
            theForm.elements[i].value = "" ;
      }
   }
}


function isDeleteOK(thelink, extraInfo, msg)
{
   if (msg == null) 
      msg = '\u3053\u306e\u9805\u76ee\u3092\u524a\u9664\u3057\u307e\u3059\u304b\u3002' ;

   var ok = confirm(msg) ;
   return ok ;
}

var maxLengthFields = null ;
var maxLengthFriendlyNames = null ;

//  input - list of text fields with max lengths in the form  
//      field1ElementName, field1MaxLength, field1FriendlyName,
//      field2ElementName, field2MaxLength, field2FriendlyName, ...
//    populates an array indexed by the form element name setting the value to the max length
//    populates an array indexed by the form element name setting the value to the friendly name
function setMaxLengthFields() {
   var fieldCount = Math.round(arguments.length / 3) ;

   maxLengthFields = new Array(fieldCount) ;
   maxLengthFriendlyNames = new Array(fieldCount) ;
   for (var i = 0; i < fieldCount; i++) {
      maxLengthFields[arguments[i*3]] = arguments[i*3+1] ;
      maxLengthFriendlyNames[arguments[i*3]] = arguments[i*3+2] ;
   }
  
}


var reqFields = null ;

//  input - list of required fields in the form  
//      field1ElementName, field1FriendlyName, field2ElementName, field2FriendlyName, ...
//    populates an array indexed by the form element name setting the value to the friendly name
function setRequiredFields() {
   var fieldCount = Math.round(arguments.length / 2) ;

   reqFields = new Array(fieldCount) ;
   for (var i = 0; i < fieldCount; i++) {
      reqFields[arguments[i*2]] = arguments[i*2+1] ;
   }
  
}


function fieldValueTooLong(formField, maxLength) {
   
   if (formField.value == null) {
      return false;
   }

   return formField.value.length > maxLength ;
}

function fieldHasNoValue(formField) {
   
   if (formField.value == null) {
      
      return formField.selectedIndex == 0 ;
   }

   return (new String(formField.value)).stringTrim().length == 0 ;
}

function validateFormFields(theForm) {
   var reqFieldList = theForm._reqFields ;
   if (reqFieldList == null) reqFieldList = reqFields ;

   var maxLengthFieldList = theForm._maxLengthFields ;
   if (maxLengthFieldList == null) maxLengthFieldList = maxLengthFields ;

   var maxLengthFriendlyNameList = theForm._maxLengthFriendlyNames ;
   if (maxLengthFriendlyNameList == null) maxLengthFriendlyNameList = maxLengthFriendlyNames ;

   var averageBytesPerChar=1;
   var missingRequiredFieldList = "" ;
   var tooLongFieldList = "" ;
   var focusReqField = null ;
   var focusMaxField = null ;

   for (var i = 0; i < theForm.elements.length; i++ ) {

      var reqFriendlyName = reqFieldList[theForm.elements[i].name] ;
      
      if (reqFriendlyName != null && fieldHasNoValue(theForm.elements[i])) {

         missingRequiredFieldList += '   ' + reqFriendlyName + '\n' ;

         if (focusReqField == null)
            focusReqField = theForm.elements[i] ;
      }

      // don't need extra validation on field length if every
      // char is 1 byte because maxlength attribute of <text> tag
      // already limits the inpute to that many chars.
      if ( (averageBytesPerChar > 1) && (maxLengthFieldList != null) ) {
         var maxLength = maxLengthFieldList[theForm.elements[i].name] ;

         if (maxLength > 0) {
            var maxChars = Math.round((maxLength - 1) / averageBytesPerChar) ;
            if (fieldValueTooLong(theForm.elements[i], maxChars)) {
               var friendlyName = maxLengthFriendlyNameList[theForm.elements[i].name] ;
               tooLongFieldList += '   ' + friendlyName + '   (' + maxChars + ')' + '\n' ;

               if (focusMaxField == null)
                  focusMaxField = theForm.elements[i] ;
            }
         }
      }

   }

   if (missingRequiredFieldList.length > 0) {
      alert('\u3053\u306e\u30d5\u30a3\u30eb\u30c9\u306b\u5024\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044:' + '\n\n' + missingRequiredFieldList) ;
      if (focusReqField != null)
         focusReqField.focus() ;
   } else {
      if (tooLongFieldList.length > 0) {
         alert('\u3053\u306e\u30d5\u30a3\u30eb\u30c9\u306e\u5024\u306f\u9577\u904e\u304e\u3066\u3044\u307e\u3059\u3002\u683c\u30d5\u30a3\u30eb\u30c9\u306e\u8a31\u53ef\u3055\u308c\u3066\u3044\u308b\u6700\u5927\u306e\u5024\u306f()\u306b\u6307\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059:' + '\n\n' + tooLongFieldList) ;
         if (focusMaxField != null)
            focusMaxField.focus() ;
      }
   }

   return ( (missingRequiredFieldList.length==0) && (tooLongFieldList.length==0) );
}

function setFormFieldNames(theForm) {
   if (!theForm) return ;

   var fieldCount = Math.round((arguments.length - 1) / 2), theField = null ;

   for (var i = 0; i < fieldCount; i++) {
      theField = theForm.elements[arguments[i*2+1]] ;
      if (theField != null) theField.friendlyName = arguments[i*2+2] ;
   }
  
}


function validateFormRequiredFields() {

   var fieldList = "", friendlyName = "" ;
   var focusField = null ;

   for (var i = 0; i < arguments.length; i++ ) {
  

      if (fieldHasNoValue(arguments[i])) {
         friendlyName = arguments[i].friendlyName || arguments[i].name ;
         fieldList += '   ' + friendlyName + '\n' ;

         if (focusField == null)
            focusField = arguments[i] ;
      }

   }

   if (fieldList.length > 0)
      alert('\u3053\u306e\u30d5\u30a3\u30eb\u30c9\u306b\u5024\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044:' + '\n\n' + fieldList) ;

   if (focusField != null)
      focusField.focus() ;

   return fieldList.length==0;
}



function testInputString(pattern, field, fieldName,required)
{   

   if (pattern == null) 
       pattern = "[^$#{}+*<>()%/-A-Za-z0-9_]";

   var found = null;
         
   if (required && field != null && field.value.replace(/ /g, "") == "") {
      alert('\u3053\u306e\u30d5\u30a3\u30eb\u30c9\u306b\u6587\u5b57\u5217\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044:' + fieldName) ;
      field.focus();
      return false ;
   }

   if (field != null && field.value.length != 0) {
      if (found = testInvalidChars(pattern ,field.value)) {
         invalidStr = new String(found);

         if (invalidStr.search(" ") != -1) {
            var tmpMsg = '{0}\u306b\u306f\u30b9\u30da\u30b9\u3092\u8a31\u53ef\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002';
            tmpMsg = tmpMsg.replace(/\{0\}/g, fieldName);
            alert(tmpMsg);
         } else {
            var tmpMsg = '{0}\u306b\u306f\u6b21\u306e\u6587\u5b57\u5217\u3092\u8a31\u53ef\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002: {1}';
            tmpMsg = tmpMsg.replace(/\{0\}/g, fieldName);
            tmpMsg = tmpMsg.replace(/\{1\}/g, found);
            alert(tmpMsg);
         }
         field.focus() ;
         return false;
      }
   }

   return true;  
}
function testInvalidChars(pattern,str)
{
   var found = null;
   var re = new RegExp(pattern, "ig");
   
   if (found = str.match(re))    
      return found;
   else    
      return null;
}


//  for the given select box, pass any number of values
//    all the option values matching a value passed in will be selected
function setSelectedOptions(selectBox) 
{
   
   // I would just use arguments.slice(1) to remove first element,
   //   but the arguments object in IE is not an array object
   var valueList = new Array(arguments.length - 1) ;
   for(var i = 1; i < arguments.length; i++)
      valueList[i+1] = arguments[i] ;
      
      
   var valueListStr = "|" + valueList.join("|") + "|" ;

   
   with (selectBox)
      for (var i = 0; i < length; i++)
         if (valueListStr.indexOf("|" + options[i].value + "|") >= 0)
            options[i].selected = true ;
}

function validUSState(stateField, countryField)
{

   if (countryField == null || countryField.value != 'US' || stateField == null)
      return true;

   var states = new Array(56);
   states[0]  = "AK";
   states[1]  = "AL";
   states[2]  = "AR";
   states[3]  = "AS";
   states[4]  = "AZ";
   states[5]  = "CA";
   states[6]  = "CO";
   states[7]  = "CT";
   states[8]  = "DC";
   states[9]  = "DE";
   states[10] = "FL";
   states[11] = "FM";
   states[12] = "GA";
   states[13] = "GU";
   states[14] = "HI";
   states[15] = "IA";
   states[16] = "ID";
   states[17] = "IL";
   states[18] = "IN";
   states[19] = "KS";
   states[20] = "KY";
   states[21] = "LA";
   states[22] = "MA";
   states[23] = "MD";
   states[24] = "ME";
   states[25] = "MH";
   states[26] = "MI";
   states[27] = "MN";
   states[28] = "MO";
   states[29] = "MS";
   states[30] = "MT";
   states[31] = "NC";
   states[32] = "ND";
   states[33] = "NE";
   states[34] = "NH";
   states[35] = "NJ";
   states[36] = "NM";
   states[37] = "NV";
   states[38] = "NY";
   states[39] = "OH";
   states[40] = "OK";
   states[41] = "OR";
   states[42] = "PA";
   states[43] = "PR";
   states[44] = "RI";
   states[45] = "SC";
   states[46] = "SD";
   states[47] = "TN";
   states[48] = "TX";
   states[49] = "UT";
   states[50] = "VA";
   states[51] = "VI";
   states[52] = "WA";
   states[53] = "WI";
   states[54] = "WV";
   states[55] = "WY";
   for (var i = 0; i < 56; i++) {
      if (stateField.value.toUpperCase() == states[i])
         return true;
   }
   
   alert('\uff12\u6841\u306e\u6709\u52b9\u306a\u5e02\u753a\u6751\u30b3\u30fc\u30c9\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002');
   stateField.focus();
   return false;

}

function isFormDeleteOK(theform, action, msg)
{
   if (msg == null)
      msg = '\u3053\u306e\u9805\u76ee\u3092\u524a\u9664\u3057\u307e\u3059\u304b\u3002';

   var ok = confirm(msg) ;

   if (ok && theform != null)
      theform.action=action;

   return ok ;
}

/*  ================================================================
    FUNCTION:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10 test.
             false, otherwise
             
   Sample Numbers:

   Credit Card         Sample Number  
   ---------------      --------------------------- 
   Visa            4111 1111 1111 1111   
   MasterCard         5500 0000 0000 0004   
   American Express   3400 0000 0000 009   
   Discover         6011 0000 0000 0004   
             
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  return ((sum % 10) == 0);
} 

function removeSpace(st) {

   return st.replace(/ /g, "") ;

}



function isCreditCardNumber(card_number, showErrorMsg) {

  var ccn = removeSpace(card_number);
  if (showErrorMsg && (ccn.length == 0) ) {
     alert('\u30ab\u30fc\u30c9\u756a\u53f7\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002');
     return false;
  }  
  if ( !isCreditCard(ccn) ) {
    if (showErrorMsg){
      var tmpMsg = '\u30ab\u30fc\u30c9\u756a\u53f7:{0}\u306f\u7121\u52b9\u3067\u3059\u3002';
      tmpMsg = tmpMsg.replace(/\{0\}/g, ccn);
      alert(tmpMsg);
    }
    return false;  
  }
  return true;
}
